import { post } from "../../../utils/request";
import OrgTreeNode from "../organization/tree-node";
import utils from "../../../utils/utils";
const env = import.meta.env;

const apiOrganization = {
  queryOrganizationListing(data?: object) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/organization.listing",
      data
    );
  },
  getOrganization(orgId: string) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/organization.get",
      {
        orgId: orgId,
      }
    );
  },
  getOrganizationExtension(orgId: string) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/organization.extension.get",
      {
        orgId: orgId,
      }
    );
  },
  updateOrganization(data?: object) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/organization.update",
      data
    );
  },
  deleteOrganization(orgId: string) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/organization.delete",
      { orgId }
    );
  },
  changeOrganizationStatus(orgId: string, status: number) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/organization.status.change",
      {
        orgId,
        status,
      }
    );
  },
  /**
   *
   * @param parentId
   * @param categoryFilter  过滤 UNIT、DEPT、默认空值显示全部
   * @returns
   */
  queryOrganizationChildrenListing(
    parentId: string,
    categoryFilter: string = ""
  ) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH +
        "/admin/api/organization.children.listing",
      {
        parentId,
        categoryFilter,
      }
    );
  },
  /**
   *
   * @param parentId
   * @param categoryFilter 过滤 UNIT、DEPT、默认空值显示全部
   * @returns
   */
  async loadChilds(
    parentId: string,
    categoryFilter: string = ""
  ): Promise<OrgTreeNode[]> {
    let treeData = Array<OrgTreeNode>();
    await apiOrganization
      .queryOrganizationChildrenListing(parentId, categoryFilter)
      .then((response: any) => {
        if (response.data.code == 200) {
          if (response.data.data != "") {
            treeData = utils.jsonArray2TypescriptArray(response.data.data);
          }
        }
      })
      .catch((err: any) => {
        utils.errorNotification(window.t("common.submitException"), err);
      });

    return treeData;
  },

  /**
   * 全部有效状态的组织机构查询接口，返回嵌套的树形 json 结构，匹配 element-plus 的 tree-select
   * @param categoryFilter  过滤 UNIT、DEPT、默认空值显示全部
   * @returns
   */
  loadAllOrganizationJson(categoryFilter: string = "") {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/organization.all.json",
      {
        categoryFilter,
      }
    );
  },
};

export default apiOrganization;
